import {getBasicUrl} from "@/config/request";

/**
 * 通用配置
 */
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/config/router.ts";

let baseUrl = getBasicUrl();
const axiosRequest = axios.create({
    baseURL: baseUrl,
    timeout: 30000,
})

/**
 * 请求拦截器
 */
axiosRequest.interceptors.request.use(
    // 在发送请求之前做些什么
    function (config) {
        const token = localStorage.getItem('token')
        token ? config.headers.token = token : null
        return config;
    },
    // 对请求错误做些什么
    function (error) {
        return Promise.reject(error);
    }
);

/**
 * 响应拦截器
 */
axiosRequest.interceptors.response.use(
    (response) => {
        let resData = response.data
        if (resData.code === 200) {
            return resData
        }else {
            ElMessage({
                type: 'warning',
                duration:2000,
                message: resData.message,
                dangerouslyUseHTMLString: true,
            })

            if (resData.code == 400){
                router.push("/")
            }
        }
    });

export default axiosRequest;